package com.songlanyun.modules.workPolicy.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.workPolicy.entity.WorkPolicy;
import com.songlanyun.modules.workPolicy.service.WorkPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/api/workpolicy")
public class ApiWorkPolicyController extends AbstractApiController {

    @Autowired
    private WorkPolicyService workPolicyService;


    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("is_show", true);
        PageUtils page = workPolicyService.queryPage(params);
        return R.ok().put("page", page);

    }


    @GetMapping("/{id}")
    public R info(@PathVariable("id") Integer id) {
        WorkPolicy workPolicy = workPolicyService.info(id);
        return R.ok().put("data", workPolicy);
    }

    /**
     * 获取优先级最高的一条创业政策，有并列的话，按时间倒序
     */
    @GetMapping("/getMinWorkPolicy")
    public R getMinWorkPolicy() {
        WorkPolicy workPolicy = workPolicyService.getOne(new LambdaQueryWrapper<WorkPolicy>()
                .eq(WorkPolicy::getIsShow, true)
                .orderByAsc(WorkPolicy::getSort)
                .orderByDesc(WorkPolicy::getCreateTime)
                .last("limit 1"));
        return R.ok().put("data", workPolicy);
    }
}
